//
//  SlideBottomCardAnimationView.swift
//  SwiftUIDemo
//
//  Created by zhouzuosong on 2020/8/23.
//  Copyright © 2020 Kael. All rights reserved.
//

import SwiftUI

struct SlideBottomCardAnimationView: View {
    
    @State var currentY : CGFloat = 0
    @GestureState private var menuOffset = CGSize.zero
    var body: some View {
        VStack(spacing: 10) {
            
            BannerText(pageTitle: "ActionSheet", text: "通过滑动手势移动底部弹出视图 来控制 视图显示隐藏", backgroundColor: .green)
            
            Spacer()
            
            VStack{
                Circle()
                    .fill(Color.green)
                    .frame(width: 100, height: 100, alignment: .center)
                    .overlay(Image(systemName: "line.horizontal.3").foregroundColor(.white).offset(x: 0, y: -20))
                    .offset(x: 0, y: -50)
                
                HStack{
                    Spacer()
                }
                Spacer()

            }
            .frame(height: 200)
            .background(RoundedRectangle(cornerRadius: 25.0).fill(Color.green))
            .offset(x: 0, y: max((currentY + menuOffset.height), 0))
            .gesture(
                DragGesture()
                    .updating($menuOffset, body: { (value, menuOffset, transaction) in
                        menuOffset = value.translation
                    })
                    .onEnded({ (value) in
                        if value.translation.height > 100 {
                            self.currentY = 200;
                        }else{
                            self.currentY = 0
                        }
                    })
            )
            .animation(.spring())
            
        }
        .edgesIgnoringSafeArea(.bottom)
        
    }
}

struct SlideBottomCardAnimationView_Previews: PreviewProvider {
    static var previews: some View {
        SlideBottomCardAnimationView()
    }
}
